package com.exchange.Public;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.widget.RemoteViews;
import com.exchange.Controller.a;
import com.exchange.View.a.a;
import com.exchange.View.a.b;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Timer;
import org.json.JSONObject;

public class DownloadingService extends Service
{
  private static ArrayList<String> b = new ArrayList();
  private NotificationManager a;

  /**
   * @deprecated
   */
  private void a(String paramString)
  {
    monitorenter;
    try
    {
      if (b.contains(paramString))
        b.remove(paramString);
      if (b.size() <= 0)
        new Timer().schedule(new j(this), 20000L);
      monitorexit;
      return;
    }
    finally
    {
      localObject = finally;
      monitorexit;
      throw localObject;
    }
  }

  /**
   * @deprecated
   */
  private void b(String paramString)
  {
    monitorenter;
    try
    {
      if (!(b.contains(paramString)))
        b.add(paramString);
      monitorexit;
      return;
    }
    finally
    {
      localObject = finally;
      monitorexit;
      throw localObject;
    }
  }

  private static boolean b(Context paramContext)
  {
    int i;
    List localList = ((ActivityManager)paramContext.getSystemService("activity")).getRunningAppProcesses();
    if (localList == null)
      i = 0;
    while (true)
    {
      ActivityManager.RunningAppProcessInfo localRunningAppProcessInfo;
      return i;
      String str = paramContext.getPackageName();
      Iterator localIterator = localList.iterator();
      do
      {
        while (!(localIterator.hasNext()))
          i = 0;
        localRunningAppProcessInfo = (ActivityManager.RunningAppProcessInfo)localIterator.next();
      }
      while ((localRunningAppProcessInfo.importance != 100) || (!(localRunningAppProcessInfo.processName.equals(str))));
      i = 1;
    }
  }

  public IBinder onBind(Intent paramIntent)
  {
    return null;
  }

  public void onCreate()
  {
    super.onCreate();
    n.c(ExchangeConstants.LOG_TAG, "onCreate'd");
    this.a = ((NotificationManager)getSystemService("notification"));
  }

  public void onDestroy()
  {
    super.onDestroy();
    n.c(ExchangeConstants.LOG_TAG, "onDestroy'd");
  }

  // ERROR //
  public void onStart(Intent paramIntent, int paramInt)
  {
    // Byte code:
    //   0: aload_0
    //   1: aload_1
    //   2: iload_2
    //   3: invokespecial 143	android/app/Service:onStart	(Landroid/content/Intent;I)V
    //   6: aload_1
    //   7: ldc 145
    //   9: invokevirtual 151	android/content/Intent:getStringExtra	(Ljava/lang/String;)Ljava/lang/String;
    //   12: astore_3
    //   13: aload_1
    //   14: ldc 153
    //   16: invokevirtual 151	android/content/Intent:getStringExtra	(Ljava/lang/String;)Ljava/lang/String;
    //   19: astore 4
    //   21: aload_1
    //   22: ldc 155
    //   24: invokevirtual 151	android/content/Intent:getStringExtra	(Ljava/lang/String;)Ljava/lang/String;
    //   27: astore 5
    //   29: getstatic 121	com/exchange/Public/ExchangeConstants:LOG_TAG	Ljava/lang/String;
    //   32: astore 6
    //   34: iconst_3
    //   35: anewarray 157	java/lang/Object
    //   38: astore 7
    //   40: aload 7
    //   42: iconst_0
    //   43: aload_3
    //   44: aastore
    //   45: aload 7
    //   47: iconst_1
    //   48: aload 4
    //   50: aastore
    //   51: aload 7
    //   53: iconst_2
    //   54: aload 5
    //   56: aastore
    //   57: aload 6
    //   59: ldc 159
    //   61: aload 7
    //   63: invokestatic 163	java/lang/String:format	(Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;
    //   66: invokestatic 129	com/exchange/Public/n:c	(Ljava/lang/String;Ljava/lang/String;)V
    //   69: aload_0
    //   70: invokevirtual 167	com/exchange/Public/DownloadingService:getApplicationContext	()Landroid/content/Context;
    //   73: invokestatic 172	com/exchange/Public/h:n	(Landroid/content/Context;)Z
    //   76: ifne +22 -> 98
    //   79: aload_0
    //   80: invokevirtual 167	com/exchange/Public/DownloadingService:getApplicationContext	()Landroid/content/Context;
    //   83: aload_0
    //   84: invokevirtual 167	com/exchange/Public/DownloadingService:getApplicationContext	()Landroid/content/Context;
    //   87: invokestatic 178	com/exchange/View/a/c:e	(Landroid/content/Context;)I
    //   90: iconst_3
    //   91: invokestatic 184	android/widget/Toast:makeText	(Landroid/content/Context;II)Landroid/widget/Toast;
    //   94: invokevirtual 187	android/widget/Toast:show	()V
    //   97: return
    //   98: getstatic 20	com/exchange/Public/DownloadingService:b	Ljava/util/ArrayList;
    //   101: astore 8
    //   103: aload 8
    //   105: monitorenter
    //   106: getstatic 20	com/exchange/Public/DownloadingService:b	Ljava/util/ArrayList;
    //   109: aload 4
    //   111: invokevirtual 33	java/util/ArrayList:contains	(Ljava/lang/Object;)Z
    //   114: ifeq +47 -> 161
    //   117: aload_0
    //   118: invokevirtual 167	com/exchange/Public/DownloadingService:getApplicationContext	()Landroid/content/Context;
    //   121: new 189	java/lang/StringBuilder
    //   124: dup
    //   125: aload_3
    //   126: invokestatic 193	java/lang/String:valueOf	(Ljava/lang/Object;)Ljava/lang/String;
    //   129: invokespecial 195	java/lang/StringBuilder:<init>	(Ljava/lang/String;)V
    //   132: ldc 197
    //   134: invokevirtual 201	java/lang/StringBuilder:append	(Ljava/lang/String;)Ljava/lang/StringBuilder;
    //   137: invokevirtual 204	java/lang/StringBuilder:toString	()Ljava/lang/String;
    //   140: iconst_3
    //   141: invokestatic 207	android/widget/Toast:makeText	(Landroid/content/Context;Ljava/lang/CharSequence;I)Landroid/widget/Toast;
    //   144: invokevirtual 187	android/widget/Toast:show	()V
    //   147: aload 8
    //   149: monitorexit
    //   150: goto -53 -> 97
    //   153: astore 9
    //   155: aload 8
    //   157: monitorexit
    //   158: aload 9
    //   160: athrow
    //   161: new 6	com/exchange/Public/DownloadingService$a
    //   164: dup
    //   165: aload_0
    //   166: aload_0
    //   167: invokevirtual 167	com/exchange/Public/DownloadingService:getApplicationContext	()Landroid/content/Context;
    //   170: aload_3
    //   171: aload 4
    //   173: aload 5
    //   175: invokespecial 210	com/exchange/Public/DownloadingService$a:<init>	(Lcom/exchange/Public/DownloadingService;Landroid/content/Context;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
    //   178: invokevirtual 215	java/lang/Thread:start	()V
    //   181: goto -34 -> 147
    //
    // Exception table:
    //   from	to	target	type
    //   106	158	153	finally
    //   161	181	153	finally
  }

  class a extends Thread
  {
    private Context b;
    private String c;
    private String d;
    private String e;
    private String f;
    private Notification g;
    private int h;
    private Handler i;

    public a(, Context paramContext, String paramString1, String paramString2, String paramString3)
    {
      this.i = new i(this);
      try
      {
        this.b = paramContext;
        this.c = paramString1;
        this.d = paramString2;
        this.e = paramString3;
        if (h.b())
        {
          this.f = ExchangeConstants.a + "/apk";
          new File(this.f).mkdirs();
          this.g = new Notification(17301633, "正在下载应用", 1L);
          RemoteViews localRemoteViews = new RemoteViews(this.b.getPackageName(), a.q(this.b));
          localRemoteViews.setProgressBar(b.G(this.b), 100, 0, false);
          localRemoteViews.setTextViewText(b.H(this.b), "0%");
          localRemoteViews.setTextViewText(b.F(this.b), "正在下载应用" + this.c);
          localRemoteViews.setTextViewText(b.I(this.b), "");
          localRemoteViews.setImageViewResource(b.s(this.b), 17301633);
          this.g.contentView = localRemoteViews;
          this.g.contentIntent = PendingIntent.getActivity(this.b, 0, new Intent(), 134217728);
          this.h = (int)System.currentTimeMillis();
          DownloadingService.a(paramDownloadingService).notify(this.h, this.g);
        }
        else
        {
          this.f = this.b.getFilesDir().getAbsolutePath();
        }
      }
      catch (Exception localException)
      {
        n.c(ExchangeConstants.LOG_TAG, localException.getMessage(), localException);
        DownloadingService.a(paramDownloadingService).cancel(this.h);
      }
    }

    private void a()
    {
      while (true)
      {
        HttpURLConnection localHttpURLConnection;
        label329: int i2;
        try
        {
          File localFile1;
          Object localObject;
          InputStream localInputStream;
          byte[] arrayOfByte;
          int j;
          int l;
          int i1;
          int i4;
          localHttpURLConnection = (HttpURLConnection)new URL(this.d).openConnection();
          localHttpURLConnection.setRequestMethod("GET");
          localHttpURLConnection.setDoOutput(true);
          localHttpURLConnection.setConnectTimeout(5000);
          String str1 = m.a(this.d) + ".apk.tmp";
          String str2 = ExchangeConstants.LOG_TAG;
          Object[] arrayOfObject1 = new Object[2];
          arrayOfObject1[0] = this.d;
          arrayOfObject1[1] = str1;
          n.c(str2, String.format("Downloading: url = %1$15s\t|\tfilename = %2$15s", arrayOfObject1));
          if (h.b())
          {
            localFile1 = new File(this.f, str1);
            localObject = new FileOutputStream(localFile1, true);
            if ((localFile1.exists()) && (localFile1.length() > 0L))
              localHttpURLConnection.setRequestProperty("Range", "bytes=" + localFile1.length() + "-");
            localHttpURLConnection.connect();
            localInputStream = localHttpURLConnection.getInputStream();
            arrayOfByte = new byte[4096];
            j = localHttpURLConnection.getContentLength();
            n.a(ExchangeConstants.LOG_TAG, "httplenth:" + String.valueOf(j));
            k = 0;
            l = 0;
            i1 = localInputStream.read(arrayOfByte);
            if (i1 > 0)
              break label329;
            i4 = 1;
          }
          while (true)
          {
            while (true)
            {
              localInputStream.close();
              ((FileOutputStream)localObject).close();
              if (i4 != 0)
                break label518;
              DownloadingService.a(this.a).cancel(this.h);
              break label784:
              this.f = this.b.getFilesDir().getAbsolutePath();
              FileOutputStream localFileOutputStream = this.b.openFileOutput(str1, 32771);
              localFile1 = this.b.getFileStreamPath(str1);
              localObject = localFileOutputStream;
            }
            ((FileOutputStream)localObject).write(arrayOfByte, 0, i1);
            l += i1;
            i2 = k + 1;
            if (k % 50 != 0)
              break label785;
            if (h.n(this.b))
              break;
            i4 = 0;
          }
          int i3 = (int)(100.0F * l / j);
          this.g.contentView.setProgressBar(b.G(this.b), 100, i3, false);
          this.g.contentView.setTextViewText(b.H(this.b), String.valueOf(i3) + "%");
          DownloadingService.a(this.a).notify(this.h, this.g);
          String str3 = ExchangeConstants.LOG_TAG;
          Object[] arrayOfObject2 = new Object[2];
          arrayOfObject2[0] = Integer.valueOf(this.h);
          arrayOfObject2[1] = Integer.valueOf(i3);
          n.c(str3, String.format("Notification: mNotificationId = %1$15s\t|\tprogress = %2$15s", arrayOfObject2));
          break label785:
          label518: File localFile2 = new File(localFile1.getParent(), localFile1.getName().replace(".tmp", ""));
          localFile1.renameTo(localFile2);
          String str4 = localFile2.getAbsolutePath();
          Message localMessage = Message.obtain();
          localMessage.obj = str4;
          this.i.sendMessage(localMessage);
        }
        catch (Exception localException1)
        {
          try
          {
            JSONObject localJSONObject = new JSONObject(this.e);
            String str5 = a.a();
            String str6 = str5.split(" ")[0];
            String str7 = str5.split(" ")[1];
            localJSONObject.put("date", str6);
            localJSONObject.put("time", str7);
            this.e = localJSONObject.toString();
            new a(this.e).start();
            break label784:
            localException1 = localException1;
            n.b(ExchangeConstants.LOG_TAG, "can not install. " + localException1.getMessage());
            this.g.contentView.setTextViewText(b.F(this.b), this.c + " 下载失败，请检查网络。");
            DownloadingService.a(this.a).notify(this.h, this.g);
            DownloadingService.a(this.a).cancel(this.h);
          }
          catch (Exception localException2)
          {
            while (true)
              localException2.printStackTrace();
          }
        }
        label784: return;
        label785: int k = i2;
      }
    }

    public void run()
    {
      DownloadingService.a(this.a, this.d);
      a();
      DownloadingService.b(this.a, this.d);
    }
  }
}